<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/25
 * Time: 16:27
 */

namespace common\models\Service;


use common\models\DAO\Blacklist;
use common\models\DAO\BlacklistRecord;
use common\models\DAO\DayAccrual;
use common\models\DAO\Whitelist;

class WindControlService {

    public static function check($userID){
        $black = Blacklist::findOne(['userID'=>$userID]);
        if($black)
            return false;
        $now = time();
        $white = Whitelist::find()->where("userID=$userID and (loseTime>$now or loseTime=0) and typeID=1")->count();
        if($white == 0){
            $frist_day = date('Y-m-01');
            $brokerage_time = strtotime("$frist_day -1 month");
            $brokerage = intval(DayAccrual::find()->where("userID=$userID and sendUserID!=0 and createTime>=$brokerage_time")->sum('invest'));
            if($brokerage >= 3000){
                $remaider = intval(DayAccrual::find()->where("userID=$userID and chargeID!=0")->sum('invest'));
                if($remaider >= 10000 && $brokerage >= 10000)
                    return true;

                if($remaider >= 1000 && $brokerage < 10000)
                    return true;
                $blackModel = new Blacklist();
                $blackModel->userID = $userID;
                $blackModel->createTime = $now;
                $b_result = $blackModel->save();
                if($b_result){
                    $blackRecordModel = new BlacklistRecord();
                    $blackRecordModel->userID = $userID;
                    $blackRecordModel->typeID = 1;
                    $blackRecordModel->createTime = $now;
                    $blackRecordModel->save();
                }
                return false;
            }
        }

        $white_cash = Whitelist::find()->where("userID=$userID and (loseTime>$now or loseTime=0) and typeID=2")->count();
        if($white_cash == 0){
            $today = strtotime(date('Y-m-d'));
            $cash = \common\models\DAO\UserCash::find()->where("userID=$userID and createTime>=$today and money<=10000")->count();
            if($cash >= 2){
                $blackModel = new Blacklist();
                $blackModel->userID = $userID;
                $blackModel->createTime = $now;
                $b_result = $blackModel->save();
                if($b_result){
                    $blackRecordModel = new BlacklistRecord();
                    $blackRecordModel->userID = $userID;
                    $blackRecordModel->typeID = 2;
                    $blackRecordModel->createTime = $now;
                    $blackRecordModel->save();
                }
                return false;
            }
            $cash = \common\models\DAO\UserCash::find()->where("userID=$userID and createTime>=$today and money>10000")->count();
            if($cash >= 3)
                return false;
        }
        return true;
    }
}